<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../lib/yzhUpload/control/css/zyUpload.css" type="text/css">
    <style>
        body {
            background-color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="role-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">公告ID</label>
        <div class="layui-input-block">
            <input type="text" name="id" lay-verify="required" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">公告标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" lay-reqtext="公告标题不能为空"
                   placeholder="请填写公告标题" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公告状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" lay-filter="type" value="0" title="启用" checked>
            <input type="radio" name="status" lay-filter="type" value="1" title="禁用">
        </div>
    </div>
    <div class="layui-form-item layui-form-text" id="text-input">
        <label class="layui-form-label">公告内容</label>
        <div class="layui-input-block">
            <textarea name="content" class="layui-textarea" placeholder="请填写公告内容"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认修改</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="https://unpkg.com/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js"></script>
<!-- 引用核心层插件 -->
<script type="text/javascript" src="../../lib/yzhUpload/core/zyFile.js"></script>
<!-- 引用控制层插件 -->
<script type="text/javascript" src="../../lib/yzhUpload/control/js/zyUpload.js"></script>
<script>

    window.onload = function () {

        let noticeId = GetQueryString("id");
        if (isNotNull(noticeId)) {
            init(noticeId);
        } else {
            const iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    }

    /**
     * 初始化数据
     */
    function init(id) {
        $.ajax({
            type: "get",
            url: '/space/notice/' + id,
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded',
            success: function (req) {
                layui.use(['form'], function () {
                    let form = layui.form
                    const noticeData = req?.data;
                    form.val('role-form', {
                        "id": noticeData?.id,
                        "title": noticeData?.title,
                        "content": noticeData?.content,
                        "status": noticeData?.status,
                    });
                })
            },
            error: function (e) {
                layer.msg("数据有误");
            }
        })
    }

    layui.use(['form'], function () {
        const form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            const index = layer.alert('确定提交修改吗？', {
                title: '提示'
            }, function () {
                let myMsg = layer.msg("正在提交...", {
                    icon: 16,
                    time: -1
                })
                const formData = data?.field;
                // 组装数据
                const noticeData = {
                    id: formData?.id,
                    title: formData?.title,
                    status: formData?.status,
                    content: formData?.content
                }
                $.ajax({
                    type: "post",
                    url: '/space/notice/edit',
                    data: JSON.stringify(noticeData),
                    dataType: 'json',
                    contentType: 'application/json; charset=UTF-8',
                    success: function (req) {
                        layer.msg("修改成功");
                        layer.close(index);
                        // 关闭弹出层
                        setTimeout(function () {
                            layer.close(myMsg);//手动关闭
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }, 1500);
                    },
                    error: function (e) {
                        layer.msg("修改失败");
                        layer.close(myMsg);//手动关闭
                    }
                })
            })
            return false;
        });
    });

</script>
</body>
</html>
